<?php

namespace IDock\Insurance\ACIC\Response;

use IDock\Http\Response;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\VehicleActualValueVisitor;

class VehicleActualValueResponse extends AllACICResponse
{

    /**
     * @var VehicleActualValueVisitor $visitor
     */
    public $visitor;

    public function setVisitor($visitor): void
    {
        $this->visitor = $visitor;
    }

    public function printerInit(): Response
    {
        $vehicleActualValuePrinter = new VehicleActualValuePrinter();
        $vehiclePrice = $this->visitor->vehiclePrice;

        $carKindCode = $this->visitor->carKindCode;
        $useNatureCode = $this->visitor->useNatureCode;
        $causeType = $this->visitor->causeType;
        $vehicleSeat = $this->visitor->vehicleSeat;

        $firstRegisterDate = $this->visitor->firstRegisterDate;

        $startDate = date('Y-m-d', strtotime($this->visitor->startDate));

        $monthDiff = $this->monthDiff($firstRegisterDate, $startDate);

        if ($vehicleSeat <= 9 and $carKindCode == '100') {
            //9座及9座以下客车
            //家庭自用 非营业
            if ($causeType == '001' or $useNatureCode == '01') {
                $vehicleActualValuePrinter->actualRate = '0.0060';
                //出租
            } elseif ($causeType == '004') {
                $vehicleActualValuePrinter->actualRate = '0.0110';
                //其他
            } else {
                $vehicleActualValuePrinter->actualRate = '0.0090';
            }
        } elseif ($vehicleSeat > 10 and $carKindCode == '100') {
            //10座及10座以上客车
            //家庭自用 非营业
            if ($causeType == '001' or $useNatureCode == '01') {
                $vehicleActualValuePrinter->actualRate = '0.0090';
                //出租
            } elseif ($causeType == '004') {
                $vehicleActualValuePrinter->actualRate = '0.0110';
                //其他
            } else {
                $vehicleActualValuePrinter->actualRate = '0.0090';
            }
        } elseif ($carKindCode == '210' or $carKindCode == '200') {
            if ($useNatureCode == '01') {
                $vehicleActualValuePrinter->actualRate = '0.0090';
                //其他
            } else {
                $vehicleActualValuePrinter->actualRate = '0.0110';
            }
        }

        //新能源
        if ($this->visitor->isNewEnergy) {
            if ($vehicleSeat <= 9 and $carKindCode == '100') {
                //9座及9座以下客车
                //家庭自用 非营业
                if ($causeType == '001' or $useNatureCode == '02') {
                    if ($vehiclePrice < 100000) {
                        $vehicleActualValuePrinter->actualRate = '0.0082';
                    }
                    if ($vehiclePrice >= 100000 and $vehiclePrice < 200000) {
                        $vehicleActualValuePrinter->actualRate = '0.0077';
                    }
                    if ($vehiclePrice >= 200000 and $vehiclePrice < 300000) {
                        $vehicleActualValuePrinter->actualRate = '0.0072';
                    }
                    if ($vehiclePrice >= 300000) {
                        $vehicleActualValuePrinter->actualRate = '0.0068';
                    }
                }
            }
        }

        $actualValue = $vehiclePrice * (1 - $monthDiff * $vehicleActualValuePrinter->actualRate);

        $vehicleActualValuePrinter->actualValue = $actualValue;

        $actualValueMostMin = $vehiclePrice * 0.2;

        $vehicleActualValuePrinter->actualValueMax = round(max($actualValue * 1.3,$actualValueMostMin),2);

        $vehicleActualValuePrinter->actualValueMin = round(max($actualValue * 0.7, $actualValueMostMin),2);

        $vehicleActualValuePrinter->actualValue = round(max($vehicleActualValuePrinter->actualValue, $vehicleActualValuePrinter->actualValueMin),2);

        $vehicleActualValuePrinter->actualRate =  (string)round($vehicleActualValuePrinter->actualRate * 100,2);

        $this->printer = $vehicleActualValuePrinter;

        return $this;
    }

    /**
     * 相差月份
     * @param $firstRegisterDate
     * @param $startDate
     * @return float|int|mixed|string
     */
    private function monthDiff($firstRegisterDate, $startDate)
    {
        $firstRegisterDate = explode('-', $firstRegisterDate);

        $startDate = explode('-', $startDate);

        $DYear = $startDate[0] - $firstRegisterDate[0];
        $DMonth = $startDate[1] - $firstRegisterDate[1];
        $DDay = $startDate[2] - $firstRegisterDate[2];

        $monthDiff = $DYear * 12 + $DMonth;

        if ($DDay < 0) {
            $monthDiff -= 1;
        }

        return $monthDiff;
    }
}
